package com.neusoft.neu24.nep.grid.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neusoft.neu24.nep.entity.ComonResult;
import com.neusoft.neu24.nep.grid.bufferedio.BufferedFileIo;
import com.neusoft.neu24.nep.grid.entity.Grid;
import com.neusoft.neu24.nep.grid.service.IGridService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;

/**
 * <p>
 * 区域网格信息 前端控制器
 * </p>
 *
 * @author 段静迪，duanjingdi@163.com
 * @since 2024-05-29
 */
@RestController
@RequestMapping("/grid")
@CrossOrigin("*")
public class GridController {
    @Autowired
    IGridService gridService;


    @GetMapping("s/all/tree")
    public  String  selectAllGridTree() throws IOException {
        String json = null;
        File file =new File("d:\\grid.json");
        if(file.exists()){
           json = BufferedFileIo.readStringFromFile();
        }else {
            ComonResult<Grid> result =  gridService.selectAllGridTree();
            if(result.getCode() == 200){
                ObjectMapper mapper = new ObjectMapper();
                json = mapper.writeValueAsString(result );
                BufferedFileIo.writeStringToFile(json);

            }
        }
        return  json;
    }

    @GetMapping("s/pid/{pid}")
    public ComonResult selectGridByPid(@PathVariable("pid")  Integer pid ) {
        return  gridService.selectGridByPid(pid);
    }

    @PostMapping("add")
    public ComonResult addGrid( @RequestBody Grid grid) {
        return  gridService.addGrid(grid);
    }

}
